package dungeonElements;

import dungeon.Dungeon;
import dungeon.RoomHandler;
import dungeonTools.Direction;
import be.kuleuven.cs.som.annotate.*;

/**
 * A class of doors. Each wall describes a wall with a door between two squares.
 * A door can be opened or closed.
 * 
 * @author Tom Jacobs & Ewoud De Moor
 * @version 3.0
 */
public class Door extends Border {
	
	/**
	 * Initialize this new door with the given square as bordering square 
	 * in the given direction and with an open or closed door.
	 * 
	 * @param square
	 * 		The bordering square of this border.
	 * @param direction
	 * 		The direction of bordering square.
	 * @param isClosed
	 * 		Determines whether the border has a open or a closed door.
	 * @effect This new wall is initialized as a border with the given square and 
	 * 		the given direction. 
	 * 		| super(square, direction)
	 * @effect The door is opened or closed.
	 * 		| if(isClosed)
	 * 		|	closeDoor()
	 * 		| else
	 * 		|	openDoor()
	 */
	@Raw
	public Door(Square square, Direction direction, boolean isClosed){
		super(square, direction);
		if(isClosed)
			closeDoor();
		else
			openDoor();
	}
	
	/**
	 * Returns whether or not the door in the wall is closed.
	 */
	@Basic
	public boolean isClosed() {
		boolean isClosed = closedDoor;
		return isClosed;
	}
	
	/**
	 * Set the door to closed.
	 */
	public void closeDoor(){
		closedDoor = true;
		for(Direction allDirections : Direction.values() ){
			if(hasSquareInDirection(allDirections) 
					&& Dungeon.isSquareInsideAnyDungeon(getSquareInDirection(allDirections)))
					RoomHandler.enforceRoomBorders((TeleportationSquare) getSquareInDirection(allDirections));
		}
	}
	
	/**
	 * Set the door to open.
	 */
	public void openDoor(){
		closedDoor = false;
		for(Direction allDirections : Direction.values() ){
			if(hasSquareInDirection(allDirections)
					&& Dungeon.isSquareInsideAnyDungeon(getSquareInDirection(allDirections)))
					RoomHandler.enforceRoomBorders((TeleportationSquare) getSquareInDirection(allDirections));
		}
	}
	
	/**
	 * Variable registering whether or not the door is closed.
	 */
	private boolean closedDoor;
	
	/**
	 * Returns whether or not this door is an obstacle.
	 * @return	True if the door is closed.
	 * 		| result == isClosed()
	 */
	@Override
	public boolean isObstacle() {
		return this.isClosed();
	}
}